package it.codelair.bakekaservice;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class LoginActivity extends Activity implements OnClickListener{
	
	private String username,password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// Show the Up button in the action bar.
		setupActionBar();
		AccountManager accountMgr = (AccountManager) getSystemService(ACCOUNT_SERVICE);
    	Account[] accounts = accountMgr.getAccountsByType("it.codelair.bakekaservice");
		if(accounts!=null && accounts.length>0){
    		Intent home = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(home);
    	}
    	else{
			final Button buttonLogin = (Button) findViewById(R.id.buttonLoginOne);
			buttonLogin.setOnClickListener(this);
    	}
	}
	
	public void onClick(View v) {
        switch ( v.getId() ) {
        	
	        case R.id.buttonLoginOne:
	        	String error = controlFields();
	        	if(error==null){
	        		AccountManager man = (AccountManager) getSystemService(ACCOUNT_SERVICE);
	    			Account account = new Account(this.username, "it.codelair.bakekaservice");
	    			if (man.addAccountExplicitly(account, null, new Bundle())) {
	    				// account registrato: creazione tabella degli enti in sqlite e inizializzare il sincronizzatore
	    				
	    				
	    				//attivo il sincronizzatore
	    				 ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
	    				 ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
	    				 ContentResolver.addPeriodicSync(account, ContactsContract.AUTHORITY, new Bundle(),432000);
	    				Intent home = new Intent(getApplicationContext(), PrincipaleActivity.class);
	    	            startActivity(home);
	    	            this.finish();
	    			} else {
	    				Toast t = Toast.makeText(v.getContext(), "Problema durante la login", Toast.LENGTH_SHORT);
	    				t.show();
	    			}
	        	}
	        	else {
	        		Toast t = Toast.makeText(v.getContext(), error, Toast.LENGTH_SHORT);
	    			t.show();
	        	}
	        	break;
        }
	}
	
	private String controlFields(){
		EditText usernameText = (EditText) findViewById(R.id.editTextUsername);
		EditText passText = (EditText) findViewById(R.id.editTextPass);
		
		if(usernameText==null || usernameText.getText()==null || (""+usernameText.getText()).equals("")
				|| passText==null || passText.getText()==null || (""+passText.getText()).equals("")){
			return "Inserire username e password";
		}
		this.setPassword(""+passText.getText());
		this.setUsername(""+usernameText.getText());
		return null;
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
//	@Override
//	public void onResume(){
//		AccountManager accountMgr = (AccountManager) getSystemService(ACCOUNT_SERVICE);
//    	Account[] accounts = accountMgr.getAccountsByType("it.codelair.bakekaservice");
//    	if(accounts!=null && accounts.length>0){
//    		Intent home = new Intent(getApplicationContext(), Splash_activity.class);
//    		this.
//            startActivity(home);
//    	}
//    	else{
//    		super.onResume();
//    	}
//	}

}
